﻿/*++

Copyright (c) 2011 Rainr Corporation

Module Name:

	Rainr.Utilities.Common.DateUtility

Abstract:

	时间工具类

Author:

	Zhou Junyu (Rainr)    2011-01-01

Revision History:
  
--*/

using System;

namespace Rainr.Utilities.Common
{
    /// <summary>
    /// 时间工具类
    /// </summary>
    public class DateUtility
    {
        /// <summary>
        /// 转换成时间随机字符串
        /// 格式：[201102021112231231111]  年月日时分秒毫秒随机数(4位)
        /// </summary>
        /// <returns>时间随机字符串</returns>
        public static string TransformRandomTime(DateTime dateTime)
        {
            return string.Format("{0}{1}{2}{3}{4}{5}{6}"
                , dateTime.Year
                , dateTime.Month < 10 ? "0" + dateTime.Month : dateTime.Month.ToString()
                , dateTime.Day < 10 ? "0" + dateTime.Day : dateTime.Day.ToString()
                , dateTime.Hour < 10 ? "0" + dateTime.Hour : dateTime.Hour.ToString()
                , dateTime.Minute < 10 ? "0" + dateTime.Minute : dateTime.Minute.ToString()
                , dateTime.Second < 10 ? "0" + dateTime.Second : dateTime.Second.ToString()
                , dateTime.Millisecond
                , new Random(dateTime.Millisecond).Next(1000, 10000)
                );
        }
        /// <summary>
        /// 转换成当前时间随机字符串
        /// 格式：[201102021112231231111]  年月日时分秒毫秒随机数(4位)
        /// </summary>
        /// <returns>前时间随机字符串</returns>
        public static string TransformCurrentRandomTime()
        {
            return TransformRandomTime(DateTime.Now);
        }

        /// <summary>
        /// 转换成时间字符串
        /// 格式：[20110202111223123]  年月日时分秒毫秒
        /// </summary>
        /// <param name="dateTime">时间对象</param>
        /// <returns>时间的字符串</returns>
        public static string TransformTime(DateTime dateTime)
        {
            return string.Format("{0}{1}{2}{3}{4}{5}{6}"
               , dateTime.Year
               , dateTime.Month < 10 ? "0" + dateTime.Month : dateTime.Month.ToString()
               , dateTime.Day < 10 ? "0" + dateTime.Day : dateTime.Day.ToString()
               , dateTime.Hour < 10 ? "0" + dateTime.Hour : dateTime.Hour.ToString()
               , dateTime.Minute < 10 ? "0" + dateTime.Minute : dateTime.Minute.ToString()
               , dateTime.Second < 10 ? "0" + dateTime.Second : dateTime.Second.ToString()
               , dateTime.Millisecond
               );
        }
        /// <summary>
        /// 转换成当前时间字符串
        /// 格式：[20110202111223123]  年月日时分秒毫秒
        /// </summary>
        /// <returns>当前时间的字符串</returns>
        public static string TransformCurrentTime()
        {
            return TransformTime(DateTime.Now);
        }

        /// <summary>
        /// 转换成分隔的时间字符串
        /// 格式：[2011-02-02 11:12:23:123]  年月日时分秒毫秒
        /// </summary>
        /// <param name="dateTime">时间对象</param>
        /// <returns>分隔的时间的字符串</returns>
        public static string TransformSplitTime(DateTime dateTime)
        {
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}:{6}"
               , dateTime.Year
               , dateTime.Month < 10 ? "0" + dateTime.Month : dateTime.Month.ToString()
               , dateTime.Day < 10 ? "0" + dateTime.Day : dateTime.Day.ToString()
               , dateTime.Hour < 10 ? "0" + dateTime.Hour : dateTime.Hour.ToString()
               , dateTime.Minute < 10 ? "0" + dateTime.Minute : dateTime.Minute.ToString()
               , dateTime.Second < 10 ? "0" + dateTime.Second : dateTime.Second.ToString()
               , dateTime.Millisecond
               );
        }
        /// <summary>
        /// 转换成当前分隔的时间字符串
        /// 格式：[2011-02-02 11:12:23:123]  年月日时分秒毫秒
        /// </summary>
        /// <returns>当前分隔的时间的字符串</returns>
        public static string TransformCurrentSplitTime()
        {
            return TransformSplitTime(DateTime.Now);
        }

    }
}
